<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolBar" style="float:left;padding-top: 10px;padding-left: 50px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="name">
          <el-input v-model="filters.name" placeholder="店铺" style="width: 10em"></el-input>
        </el-form-item>
        <el-form-item prop="platform">
          <el-select v-model="filters.platform" :clearable="true" placeholder="站点" style="width: 10em">
            <el-option v-for="item in platformData"
                       :key="item.label"
                       :label="item.value"
                       :value="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-search"
                     :label="$t('action.search')"
                     perms="basic:shop:view"
                     @click="findPage"/>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-refresh"
                     :label="$t('action.reset')"
                     perms="basic:shop:view"
                     @click="reset"/>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-plus"
                     :label="$t('action.add')"
                     perms="basic:shop:add"
                     @click="showAdd"/>
        </el-form-item>
      </el-form>
    </div>

    <!--表格内容栏-->
    <kt-table perms-edit="basic:shop:edit" perms-delete="basic:shop:delete" :data="pageResult" :columns="filterColumns"
              @findPage="findPage" @handleEdit="showEdit" @handleDelete="handleDelete">
    </kt-table>

    <!--新增编辑界面-->
    <el-dialog :title="operation?'新增':'编辑'"
               width="30%"
               :visible.sync="dialogVisible"
               :close-on-click-modal="false">
      <el-form :model="dataForm"
               label-width="100px"
               :rules="dataFormRules"
               ref="dataForm"
               :size="size"
               label-position="right">
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺" prop="name">
          <el-input v-model="dataForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="站点" prop="platform">
          <el-select v-model="dataForm.platform" :clearable="true" placeholder="选择站点" style="width: 100%">
            <el-option v-for="item in platformData"
                       :key="item.label"
                       :label="item.value"
                       :value="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绑定邮箱" prop="bindEmail">
          <el-input v-model="dataForm.bindEmail" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="绑定手机" prop="bindMobile">
          <el-input v-model="dataForm.bindMobile" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="浏览器IP" prop="superIp">
          <el-input v-model="dataForm.superIp" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="绑定的公司" prop="registeredCompany">
          <el-input v-model="dataForm.registeredCompany" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="公司法人" prop="corporate">
          <el-input v-model="dataForm.corporate" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button :size="size" @click.native="dialogVisible=false">{{$t('action.cancel')}}</el-button>
        <el-button :size="size" type="primary" :loading="editLoading" @click.native="submitForm">
          {{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import KtTable from '../core/KtTable'
  import KtButton from '../core/KtButton'
  import TableColumnFilterDialog from '../core/TableColumnFilterDialog'

  export default {
    name: "Shop",
    components: {
      KtTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() {
      return {
        size: 'small',
        filters: {
          name: '',
          platform: ''
        },
        columns: [],//初始表格列显示字段
        filterColumns: [],//列显示过滤字段
        pageRequest: {},
        pageResult: {},
        operation: false,//true新增 false编辑
        dialogVisible: false,//新增编辑界面是否显示
        editLoading: false,
        //新增或编辑页面
        dataForm: {
          id: 0,
          name: '',
          platform: '',
          bindEmail: '',
          bindMobile: '',
          superIp: '',
          registeredCompany: '',
          corporate: '',
          remark: ''
        },
        dataFormRules: {
          name: [
            {required: true, message: '请输入平台店铺', trigger: 'blur'}
          ],
          platform: [
            {required: true, message: '请选择站点', trigger: 'blur'}
          ],
          bindEmail: [
            {required: true, message: '请关联绑定的邮箱', trigger: 'blur'}
          ],
          bindMobile: [
            {required: true, message: '请关联绑定的手机号码', trigger: 'blur'}
          ],
          superIp: [
            {required: true, message: '请关联超级浏览器IP', trigger: 'blur'}
          ],
          registeredCompany: [
            {required: true, message: '请输入注册店铺的公司', trigger: 'blur'}
          ],
          corporate: [
            {required: true, message: '请输入公司法人名称', trigger: 'blur'}
          ]
        },
        platformData: [
          {label: 'US', value: '美国'}
        ]
      }
    },
    methods: {
      // 处理表格列过滤显示
      initColumns() {
        this.columns = [
        //  {prop: "id", label: "ID", minWidth: 50},
          {prop: "name", label: "店铺", minWidth: 100},
          {prop: "platformName", label: "站点", minWidth: 100},
          {prop: "bindEmail", label: "绑定邮箱", showOverflow: true, minWidth: 130},
          {prop: "bindMobile", label: "绑定手机", minWidth: 130},
          {prop: "superIp", label: "浏览器IP", showOverflow: true, minWidth: 130},
          {prop: "registeredCompany", label: "注册的公司", showOverflow: true, minWidth: 150},
          {prop: "corporate", label: "公司法人", minWidth: 130},
          {prop: "remark", label: "备注", showOverflow: true, minWidth: 130},
          {prop: "lastUpdator", label: "最近操作者", minWidth: 130},
          {prop: "lastUpdateTime", label: "最近操作时间", minWidth: 140}
        ];
        this.filterColumns = JSON.parse(JSON.stringify(this.columns));
      },
      //处理表格列过滤显示
      displayFilterColumnDialog() {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns(data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      //获取分页数据
      findPage(data) {
        if ((data !== null && typeof data !== 'undefined') && typeof data.pageRequest !== 'undefined') {
          this.pageRequest = data.pageRequest;
        }
        this.pageRequest.params = {
          name: this.filters.name,
          platform: this.filters.platform
        };
        this.$api.shop.findPage(this.pageRequest).then((res) => {
          if (res.code === 200) {
            this.pageResult = res.data;
            this.convertPlatformName(this.pageResult);
          }
        }).then((data !== null && typeof data !== 'undefined') ? data.callback : '');
      },
      findPlatform() {
        this.$api.dict.findByType({type: 'nation'}).then((res) => {
          if (res.code === 200) {
            this.platformData = res.data;
          }
        })
      },
      //平台名称转换
      convertPlatformName(pageResult) {
        let shopList = pageResult.content;
        shopList.forEach(shop => {
          this.platformData.forEach(pt => {
            if (shop.platform === pt.label) {
              shop.platformName = pt.value;
            }
          })
        })
      },
      //批量删除
      handleDelete(data) {
        this.$api.shop.batchDelete(data.params).then(data !== null ? data.callback : '');
      },
      //显示新增界面
      showAdd() {
        this.dialogVisible = true;
        this.operation = true;
      },
      //显示编辑界面
      showEdit(params) {
        this.dialogVisible = true;
        this.operation = false;
        this.dataForm = Object.assign({}, params.row);
      },
      //提交表单
      submitForm() {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗?', '提示', {}).then(() => {
              this.editLoading = true;
              // let params = Object.assign({},this.dataForm);
              this.$api.shop.save(this.dataForm).then((res) => {
                this.editLoading = false;
                if (res.code === 200) {
                  this.$message({message: '操作成功', type: 'success'});
                  this.dialogVisible = false;
                  this.$refs['dataForm'].resetFields();
                } else {
                  this.$message({message: res.message, type: 'error'});
                }
                this.findPage();
              });
            })
          }
        })
      },
      //重置
      reset() {
        this.$refs['filters'].resetFields();
        this.findPage();
      }
    },
    mounted() {
      this.initColumns();
      this.findPlatform();
    }
  }
</script>

<style scoped>

</style>
